<?php
class ControllerUploadEditProduct extends Controller {
	private $error = array();
	public function index()
	{
		//-------------------------------------------------------------------------//
		// Check login
		//-------------------------------------------------------------------------//
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->https('upload/edit_product');
			$this->redirect($this->url->https('account/login'));
		}
		//-------------------------------------------------------------------------//
		// get product id from parameter
		//-------------------------------------------------------------------------//
		if(isset($this->request->post["product_id"]) == false){
			$this->redirect($this->url->http('common/home'));
		}
		$product_id = $this->request->post["product_id"];
		$this->data["product_id"] = $product_id;
		//-------------------------------------------------------------------------//
		// load module
		//-------------------------------------------------------------------------//
		$this->language->load('common/header');
		$this->language->load('common/home');
		$this->load->model('product/select');
		$this->load->model('upload/shop');
		$this->load->model('upload/product');
		$this->data['action'] = $this->url->http('upload/product/update');

		//-------------------------------------------------------------------------//
		// get parent catalog
		//-------------------------------------------------------------------------//
		$catalog_arr = $this->model_upload_product->getParentCategories();
		array_unshift($catalog_arr, array("catalog_id" => "","name"=>"Chọn Danh Mục Cấp 1"));
		$this->data['parent_catalog'] = $catalog_arr;
		//-------------------------------------------------------------------------//
		// Get product infomation base on productId which id got from parameter
		//-------------------------------------------------------------------------//
		$productInfo = $this->model_product_select->getProductsByProductId1($product_id);

		//-------------------------------------------------------------------------//
		// get list ofcatalog incase update product
		//-------------------------------------------------------------------------//
		if(empty($productInfo[0]['category_id']) == false){
			$this->load->model('product_to_catalog/select');
	   		$str    = $this->model_product_to_catalog_select->getListCategories($product_id);
	   		$childCatalog = array();
			if(mb_strlen($str) > 0){
				$childCatalog = explode("_", $str);
			}
		}
		if(empty($childCatalog) == false){
			unset($childCatalog[0]);
		}
		//-------------------------------------------------------------------------//
		// load library
		//-------------------------------------------------------------------------//
		$this->load->helper('image');
		$this->load->model('localisation/zone');
		//----------------------------------------------------------------------------------//
		// get area
		//----------------------------------------------------------------------------------//
		//$are = array("zone_id" => "", "name" => "Chon Khu Vuc");
    	$area = $this->model_localisation_zone->getZonesByCountryId(230);
		//$area = array_pad($area, - (count($area) + 1), $are);
    	$this->data['areas']  = $area;

		//-------------------------------------------------------------------------//
		// get catalog
		//-------------------------------------------------------------------------//
		$this->data['pa_catalog_id'] = $productInfo[0]['category_id'];
		if(empty($childCatalog) == false){
			$this->data['cr_catalog_id'] = $childCatalog[1];
		}
		$this->data['price']         = $productInfo[0]['price'];
		$this->data['sale_flg']      = $productInfo[0]['sale_flg'];
		$this->data['payment_id']    = $productInfo[0]['payment_id'];
		$this->data['description']   = $productInfo[0]['description'];
		$this->data['zone_id']       = $productInfo[0]['zone_id'];
		$this->data['old_image']     = $productInfo[0]['image'];
		$imageName                   = $productInfo[0]['image'];
		$this->data['image']         = "";
		if(empty($imageName) == false){
			$this->data['image'] = image_resize($imageName, $this->config->get('config_image_related_width'), $this->config->get('config_image_related_height'));
		}
		$this->data["productInfo"] = $productInfo[0];
		$this->id       = 'content';
		$this->template = $this->config->get('config_template') . 'uploadtosell/edit_product.phtml';
		$this->layout   = 'common/layout_uploadtosell';
		$this->render();
	}
}
?>